export function animationFrame (fn) {
    if (!window.requestAnimationFrame) {
        window.requestAnimationFrame = (
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function (callback) {
                return window.setTimeout(callback, 1000 / 60)
            }
        )
    }
    window.requestAnimationFrame(fn)
}
export function scrollTop (el, from = 0, to, duration = 500, endCallback) {
    const difference = Math.abs(from - to)
    const step = duration ? Math.ceil(difference / duration * 50) : 60

    function scroll (start, end, step) {
        if (start === end) {
            endCallback && endCallback()
            return
        }

        let d = (start + step > end) ? end : start + step
        if (start > end) {
            d = (start - step < end) ? end : start - step
        }

        if (el === window) {
            window.scrollTo(d, d)
        } else {
            el.scrollTop = d
        }
        animationFrame(() => scroll(d, end, step))
    }
    scroll(from, to, step)
}
